package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiU_advert_position_schedule;

import cn.com.api.service.ApiU_advert_position_scheduleService;

import cn.com.util.*;


public class U_advert_position_scheduleBaseController {

	private static Logger logger = Logger.getLogger(U_advert_position_scheduleBaseController.class);
	
	@Autowired
	private ApiU_advert_position_scheduleService apiU_advert_position_scheduleService;
	
	/**
	 * 列表
	 * @return
	 */
	@RequestMapping(value = "u_advert_position_schedule-list")
	public ModelAndView u_advert_position_schedule_list(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/u_advert_position_schedule_list.jsp");
		
		List<ApiU_advert_position_schedule> u_advert_position_schedules = apiU_advert_position_scheduleService.findU_advert_position_schedules();
		mv.addObject("u_advert_position_schedules", u_advert_position_schedules);
		
		return mv;
	}
	
	/**
	 * 进入新增或者编辑页面
	 * @return
	 */
	@RequestMapping(value = "u_advert_position_schedule-add-pre")
	public ModelAndView u_advert_position_schedule_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/u_advert_position_schedule_add.jsp");
		//
		if(request.getParameter("id")!=null){
			String id = request.getParameter("id");
			ApiU_advert_position_schedule u_advert_position_schedule = apiU_advert_position_scheduleService.findU_advert_position_scheduleById(Integer.valueOf(id));
			mv.addObject("u_advert_position_schedule",u_advert_position_schedule);
		}
		return mv;
	}

	/**
	 *新增或者编辑
	 * @return
	 */
	@RequestMapping(value = "u_advert_position_schedule-add")
	public void u_advert_position_schedule_add(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String id = request.getParameter("id");
		String position_id = request.getParameter("position_id");
		String start_time = request.getParameter("start_time");
		String end_time = request.getParameter("end_time");
		
		Map<String,Object> map = new HashMap<String,Object>();
		//String id = request.getParameter("id");
		//add
		if(id==null || (id.trim()).equals("")){
			ApiU_advert_position_schedule u_advert_position_schedule = new ApiU_advert_position_schedule();
            /*
		    u_advert_position_schedule.setId(id);
		    u_advert_position_schedule.setPosition_id(position_id);
		    u_advert_position_schedule.setStart_time(start_time);
		    u_advert_position_schedule.setEnd_time(end_time);
	        */
			apiU_advert_position_scheduleService.insertU_advert_position_schedule(u_advert_position_schedule);
		}
		//edit
		else{
			ApiU_advert_position_schedule u_advert_position_schedule = apiU_advert_position_scheduleService.findU_advert_position_scheduleById(Integer.valueOf(id));
	        /*
		    u_advert_position_schedule.setId(id);
		    u_advert_position_schedule.setPosition_id(position_id);
		    u_advert_position_schedule.setStart_time(start_time);
		    u_advert_position_schedule.setEnd_time(end_time);
			*/
			apiU_advert_position_scheduleService.updateU_advert_position_schedule(u_advert_position_schedule);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
        JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	/**
	 *删除
	 * @return
	 */
	@RequestMapping(value = "u_advert_position_schedule-delete")
	public void u_advert_position_schedule_delete(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		
		//delete
		int ret_i =	apiU_advert_position_scheduleService.deleteU_advert_position_schedule(Integer.valueOf(id));
		
		//
		if(ret_i > 0){
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
}



